package com.bilibili.sys.dao.impl;

import com.bilibili.sys.bean.SysMenu;
import com.bilibili.sys.dao.IMenuDao;
import com.bilibili.sys.utils.MyDBUtils;
import com.bilibili.sys.utils.PageUtils;
import com.bilibili.sys.utils.StringUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author alpha
 * @program: BookSystem
 * @description:
 * @since 2024-03-28 15:33
 **/

public class MenuDaoImpl implements IMenuDao {
    @Override
    public List<SysMenu> list(SysMenu entity) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select * from sys_menu order by seq asc ";
        try {
            //BeanListHandler会自动帮助我们完成字段和属性的映射，前提是属性和字段完全一致
            //List<SysUser> list = queryRunner.query(sql, new BeanListHandler<SysUser>(SysUser.class));
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    List<SysMenu> list = new ArrayList<>();
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity);
                    }
                    return list;
                }
            });
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<SysMenu> listPage(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select * from sys_menu  where parent_id = -1 limit ?,?";
        if (StringUtils.isNotEmpty(pageUtils.getKey())) {
            //需要带条件查询
            sql = "select * from sys_menu  where parent_id = -1 and name like '%" + pageUtils.getKey() + "%'  limit ?,?";
        }
        //计算分页开始的位置（前端传递过来从1开始）
        int startNo = pageUtils.getStart();
        try {
            //BeanListHandler会自动帮助我们完成字段和属性的映射，前提是属性和字段完全一致
            //List<SysUser> list = queryRunner.query(sql, new BeanListHandler<SysUser>(SysUser.class));
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    List<SysMenu> list = new ArrayList<>();
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity);
                    }
                    return list;

                }
            }, startNo, pageUtils.getPageSize());//从pageUtils位置开始，取getPageSize条记录
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int save(SysMenu entity) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "insert into sys_menu(name,url,parent_id,seq) values (?,?,?,?)";
        try {
            return queryRunner.update(sql, entity.getName(), entity.getUrl(), entity.getParentId(), entity.getSeq());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public SysMenu findById(int id) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select * from sys_menu where id =?";
        try {
            //BeanListHandler会自动帮助我们完成字段和属性的映射，前提是属性和字段完全一致
            //List<SysUser> list = queryRunner.query(sql, new BeanListHandler<SysUser>(SysUser.class));
            return queryRunner.query(sql, new ResultSetHandler<SysMenu>() {
                @Override
                public SysMenu handle(ResultSet resultSet) throws SQLException {
                    if (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        return entity;
                    }
                    return null;
                }
            }, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateById(SysMenu entity) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "update sys_menu set name = ?,url = ?,parent_id = ?,seq = ? where id = ?";
        try {
            return queryRunner.update(sql, entity.getName(), entity.getUrl(), entity.getParentId(), entity.getSeq(), entity.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteById(int id) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "delete from sys_menu  where id = ?";
        try {
            return queryRunner.update(sql, id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int count(PageUtils pageUtils) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select count(*) from sys_menu";
        if (StringUtils.isNotEmpty(pageUtils.getKey())) {
            sql = "select count(*) from sys_menu where name like '%" + pageUtils.getKey() + "%' or notes  like '%" + pageUtils.getKey() + "%' ";
        }
        try {
            return queryRunner.query(sql, new ResultSetHandler<Integer>() {
                @Override
                public Integer handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    return resultSet.getInt(1);
                }
            });
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<SysMenu> getAllParent() {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select * from sys_menu where parent_id = -1";
        try {
            //BeanListHandler会自动帮助我们完成字段和属性的映射，前提是属性和字段完全一致
            //List<SysUser> list = queryRunner.query(sql, new BeanListHandler<SysUser>(SysUser.class));
            List<SysMenu> list = queryRunner.query(sql, new ResultSetHandler<List<SysMenu>>() {
                @Override
                public List<SysMenu> handle(ResultSet resultSet) throws SQLException {
                    List<SysMenu> list = new ArrayList<>();
                    while (resultSet.next()) {
                        SysMenu entity = new SysMenu();
                        entity.setId(resultSet.getInt("id"));
                        entity.setName(resultSet.getString("name"));
                        entity.setUrl(resultSet.getString("url"));
                        entity.setParentId(resultSet.getInt("parent_id"));
                        entity.setSeq(resultSet.getInt("seq"));
                        entity.setCreateTime(resultSet.getDate("create_time"));
                        list.add(entity);
                    }
                    return list;
                }
            });
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public boolean isDispatcher(int id) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select count(*) from sys_role_menu  where menu_id = ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<Boolean>() {
                @Override
                public Boolean handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    int count = resultSet.getInt(1);
                    return count > 0 ? true : false;
                }
            },id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean haveSubMenu(int id) {
        QueryRunner queryRunner = MyDBUtils.getQueryRunner();
        String sql = "select count(*) from sys_menu where parent_id = ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<Boolean>() {
                @Override
                public Boolean handle(ResultSet resultSet) throws SQLException {
                    resultSet.next();
                    int count = resultSet.getInt(1);
                    return count > 0 ? true : false;
                }
            },id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }
}
